package com.yeepay.yjpay;

import java.util.ResourceBundle;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.yeepay.util.common.RandomUtil;

/**
 * 易宝支付接口配置信息类
 * 
 * @author hyr
 *
 */
public class YeepayApiConfig {

	private static final Log Logger = LogFactory.getLog(YeepayApiConfig.class);
	
	/**
	 * 证件类型:身份证
	 */
	public static final String ID_CARD_TYPE_IDENTITY_CARD = "01";
	
	/**
	 * 货币类型：人民币
	 */
	public static final Integer CURRENCY_RENMINBI = 156;
	
	/**
	 * 用户标识类型：用户ID
	 */
	public static final Integer IDENTITY_TYPE_USER_ID = 2;
	
	/**
	 * 终端类型：其他
	 */
	public static final Integer TERMINAL_TYPE_OTHER = 3;
	
	
	/**
	 * 商品类型：金融投资
	 */
	public static final String PRODUCT_CATALOG_FINANCE = "18";
	
	/**
	 * 订单有效期，以分为单位
	 */
	public static final Integer ORDER_EXPDATE = 10;

	/**
	 * 支付方式:借记卡
	 */
	public static final String PAYTYPE_DEBIT_CARD = "1";
	
	private String configFile; // 配置文件地址 
	private ResourceBundle configFileResource; // 配置文件资源对象
	private String payWebUrlprefix; // PC端接口URL前缀
	private String payMobileUrlprefix; // 移动端接口URL前缀
	private String payUrlprefix; // 支付API接口URL前缀
	private String merchantUrlprefix; // 商户通用接口URL前缀
	private String merchantAccount; // 商户账户编号
	private String merchantPublickey; // 商户公钥
	private String merchantPrivatekey; // 商户私钥
	private String yibaoPublickey; // 易宝公钥
	private String callbackUrl; // 商户后台系统的回调地址
	private String mobileCallbackUrl; // 商户移动端前台系统提供的回调地址
	private String mobileCallbackUrlV3; // 商户移动端前台系统提供的回调地址V3版本
	private String mobileCallbackUrlV4; // 商户移动端前台系统提供的回调地址V4版本
	private String h5CallbackUrl; // H5端前台系统提供的回调跳转地址
	private String comfirmPayUrl; // 短信确认支付URL地址
	
	public YeepayApiConfig(String configFile) {

		Logger.debug("加载易宝支付配置文件：" + configFile);
		this.configFile = configFile;
	}
	
	private ResourceBundle getConfigFileResource() {
		if (this.configFileResource == null) {
			this.configFileResource = ResourceBundle.getBundle(configFile);
		}
		return configFileResource;
	}

	public String getMerchantAccount() {
		if (StringUtils.isEmpty(this.merchantAccount)) {
			this.merchantAccount = this.getConfigFileResource().getString("payapi.paytest_merchantaccount");
		}
		return merchantAccount;
	}

	public String getPayWebUrlprefix() {
		if (StringUtils.isEmpty(payWebUrlprefix)) {
			this.payWebUrlprefix = this.getConfigFileResource().getString("payapi.payweb_urlprefix");
		}
		return payWebUrlprefix;
	}

	public String getPayMobileUrlprefix() {
		if (StringUtils.isEmpty(payMobileUrlprefix)) {
			this.payMobileUrlprefix = this.getConfigFileResource().getString("payapi.paymobile_urlprefix");
		}
		return payMobileUrlprefix;
	}

	public String getPayUrlprefix() {
		if (StringUtils.isEmpty(payUrlprefix)) {
			this.payUrlprefix = this.getConfigFileResource().getString("payapi.pay_urlprefix");
		}
		return payUrlprefix;
	}

	public String getMerchantUrlprefix() {
		if (StringUtils.isEmpty(merchantUrlprefix)) {
			this.merchantUrlprefix = this.getConfigFileResource().getString("payapi.merchant_urlprefix");
		}
		return merchantUrlprefix;
	}

	public String getMerchantPublickey() {
		if (StringUtils.isEmpty(merchantPublickey)) {
			this.merchantPublickey = this.getConfigFileResource().getString("payapi.paytest_merchant_publickey");
		}
		return merchantPublickey;
	}

	public String getMerchantPrivatekey() {
		if (StringUtils.isEmpty(merchantPrivatekey)) {
			this.merchantPrivatekey = this.getConfigFileResource().getString("payapi.paytest_merchant_privatekey");
		}
		return merchantPrivatekey;
	}

	public String getYibaoPublickey() {
		if (StringUtils.isEmpty(yibaoPublickey)) {
			this.yibaoPublickey = this.getConfigFileResource().getString("payapi.paytest_yibao_publickey");
		}
		return yibaoPublickey;
	}

	public String getCallbackUrl() {
		if (StringUtils.isEmpty(callbackUrl)) {
			this.callbackUrl = this.getConfigFileResource().getString("payapi.callback_url");
		}
		return callbackUrl;
	}

	public String getMobileCallbackUrl() {
		if (StringUtils.isEmpty(mobileCallbackUrl)) {
			this.mobileCallbackUrl = this.getConfigFileResource().getString("payapi.mobile_callback_url");
		}
		return mobileCallbackUrl;
	}

	public String getH5CallbackUrl() {
		if (StringUtils.isEmpty(h5CallbackUrl)) {
			this.h5CallbackUrl = this.getConfigFileResource().getString("h5.callback_url");
		}
		return h5CallbackUrl;
	}

	public String getMobileCallbackUrlV3() {
		if (StringUtils.isEmpty(mobileCallbackUrlV3)) {
			this.mobileCallbackUrlV3 = this.getConfigFileResource().getString("payapi.mobile_callback_url_v3");
		}
		return this.mobileCallbackUrlV3;
	}
	
	public String getMobileCallbackUrlV4() {
		if (StringUtils.isEmpty(mobileCallbackUrlV4)) {
			this.mobileCallbackUrlV4 = this.getConfigFileResource().getString("payapi.mobile_callback_url_v4");
		}
		return this.mobileCallbackUrlV4;
	}
	
	/**
	 * 生成商户随机AESkey
	 * @return
	 */
	public static String GenerateMerchantAesKey() {
		return RandomUtil.getRandom(16);
	}

	/**
	 * 短信确认支付URL地址
	 * @return
	 */
	public String getComfirmPayUrl() {
		if (StringUtils.isEmpty(comfirmPayUrl)) {
			this.comfirmPayUrl = this.getConfigFileResource().getString("h5.comfirm_pay_url");
		}
		return this.comfirmPayUrl;
	}
}
